//
//  AccountViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/2.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "AccountViewController.h"
#import "AliPayTableViewCell.h"
#import "BlankTableViewCell.h"
#import "AddNewAccoutViewController.h"
#import "CheckBalnkViewController.h"

@interface AccountViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray *bankArray;
@property (nonatomic, strong) UIView *tempView;

@end

@implementation AccountViewController

-(NSMutableArray*)bankArray{
    if (!_bankArray) {
        _bankArray=[NSMutableArray array];
    }
    return _bankArray;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self showNav];
    [self.bankArray removeAllObjects];
    [self loadbankListRequest];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=MyString(@"我的账户");
    [self creatTablew];
    [self viewIsDetemp];
}
-(void)createView{
    UIButton *Ybtn=[[UIButton alloc]init];
    [Ybtn setTitle:@"+ 添加新卡" forState:UIControlStateNormal];
    [Ybtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [Ybtn addTarget:self action:@selector(AddBlankClick) forControlEvents:UIControlEventTouchUpInside];
    Ybtn.backgroundColor=Red_Color;
    Ybtn.titleLabel.font=FontAndStyle15;
    [self.view addSubview:Ybtn];
    [Ybtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom);
        make.right.left.equalTo(0);
        make.height.equalTo(50);
        
    }];
    
}
//有数据
-(void)creatTablew{
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MainW, MainH-64-50) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.backgroundColor=Gray_Color;
    tableView.dataSource = self;
    tableView.separatorStyle=0;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    [tableView registerNib:[UINib nibWithNibName:@"BlankTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"BlankTableViewCell"];
    [tableView registerNib:[UINib nibWithNibName:@"AliPayTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"AliPayTableViewCell"];
    

}
#pragma mark -------- tableviewdelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.bankArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 107;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
        BlankTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"BlankTableViewCell"];
        if (!cell) {
            cell = [tableView dequeueReusableCellWithIdentifier:@"BlankTableViewCell"];
        }
    cell.name.text=[NSString stringWithFormat:@"%@",self.bankArray[indexPath.section][@"creditName"]];
     cell.type.text=[NSString stringWithFormat:@"%@",self.bankArray[indexPath.section][@"creditBank"]];
     cell.count.text=[NSString stringWithFormat:@"%@",self.bankArray[indexPath.section][@"creditAccount"]];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.bankArray[indexPath.section][@"logo"]]]];
    NSString*str=cell.count.text;
    NSString*replacedStr;
    if (cell.count.text.length==16) {
        replacedStr= [str stringByReplacingOccurrencesOfString: [str substringWithRange:NSMakeRange(4,str.length-8)]withString:@" **** **** "];
    }else{
        replacedStr= [str stringByReplacingOccurrencesOfString: [str substringWithRange:NSMakeRange(4,str.length-7)]withString:@" **** **** **** "];
    }
    cell.count.text=replacedStr;
    return cell;
    
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    //在这里实现删除操作
    [self loadremovebankListRequest:[NSString stringWithFormat:@"%@",self.bankArray[indexPath.section][@"bankAccountId"]]];
    //删除数据，和删除动画
//    [self.myDataArr removeObjectAtIndex:deleteRow];
    //[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.section inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
}


#pragma mark ---------- 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary*dic=self.bankArray[indexPath.section];
    if (self.chooseBank){
        self.chooseBank(dic);
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        //NSLog(@"点击");
    }

    
    
}


//无数据
-(void)viewIsDetemp{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, MainW, MainH-64-40)];
    self.tempView=view;
    self.tempView.hidden=YES;
    [self.view addSubview:view];
    UIImageView *imagView=[[UIImageView alloc]initWithFrame:CGRectMake(MainW/2-90, 75, 180, 210)];
    imagView.image=[UIImage imageNamed:@"img_blank_monk"];
    [view addSubview:imagView];
    UILabel* label=[[UILabel alloc]init];
    label.text=MyString(@"暂无账户绑定");
    label.font=FontAndStyle14;
    label.textAlignment=1;
    label.textColor=TextColor6;
    [view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(imagView.mas_bottom).mas_offset(15);
        make.left.right.mas_equalTo(0);
        make.height.equalTo(20);
    }];
    
   
    
}
//添加新的银行卡
-(void)AddBlankClick{
    CheckBalnkViewController*vc=[[CheckBalnkViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
//添加新的微信跟支付宝账户
-(void)addNewAccoutClick:(FL_Button *)sender{
    AddNewAccoutViewController *vc=[[AddNewAccoutViewController alloc]init];
    vc.styple=sender.tag;
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(void)loadbankListRequest{
    
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    [self doHttpRequestWithApi:BANKCARDLIST method:GET dic:dic successBlock:^(NSDictionary *dic) {
        [self.bankArray addObjectsFromArray:dic[@"data"]];
        [self.tableView reloadData];
        if (self.bankArray.count==0) {
            self.tempView.hidden=NO;
        }else{
            self.tempView.hidden=YES;
            
        }
        
    }];
}
-(void)loadremovebankListRequest:(NSString *)bankAccountId{
    [self.bankArray removeAllObjects];
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"bankAccountId"]=bankAccountId;
    [self doHttpRequestWithApi:UNBINDBANKCARD method:GET dic:dic successBlock:^(NSDictionary *dic) {
        
        [self loadbankListRequest];
    }];
}
@end
